home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dirut / cdd.zip / CDD.C next >
Text File  |  1987-10-11  |  5KB  |  196 lines

  1. /*****************************************************************************\
  2. *                                                                             *
  3. *   CDD, a substitute for MESSDOS' CD command                                 *
  4. *                                                                             *
  5. *   (c) 1987 Wolfgang Siebeck                                                 *
  6. *                                                                             *
  7. *   usage: CDD [directory name | $environment variable]                       *
  8. *   changes the directory like MSDOS CD should do                             *
  9. *                                                                             *
  10. *   CDD [no argument] gets the directory name from the environment            *
  11. *       variable HOME.                                                        *
  12. *                                                                             *
  13. *   CDD $[variable] gets the directory from variable                          *
  14. *                                                                             *
  15. *   CDD [d:]dirname changes the directory and - if given - the default drive  *
  16. *                                                                             *
  17. *   wildcards in dirname are supported !                                      *
  18. *                                                                             *
  19. *   (This is formatted for \t == "    ")                                      *
  20. *                                                                             *
  21. \*****************************************************************************/
  22.  
  23.  
  24. extern char far **environ;
  25. char *getenv();
  26.  
  27. #include <stdio.h>
  28. #include <dir.h>
  29. /*
  30. #include <dos.h>
  31. */
  32.  
  33. main (argc,argv)
  34. int argc;
  35. char *argv[];
  36.  
  37. {
  38.  
  39.     char newdir[64], errtext[32];        /* here goes the directory        */
  40.     char *tmp;
  41.     register int i;
  42.  
  43.     if (argc == 2) {                    /* a directory name is given    */
  44.         strcpy(newdir,argv[1]);
  45.     } else {
  46.         strcpy(newdir,"$HOME");            /* if no argument given, lets    */
  47.     }                                    /* go to $HOME                    */
  48.  
  49.     if (newdir[0] == '$') {
  50.         moveleft (newdir,1,0);            /* get rid of the $-sign and    */
  51.         for (i=0;                        /* remember ? all UPPERcase !    */
  52.             (newdir[i] = toupper(newdir[i])) != '\0';
  53.             i++);
  54.         tmp = getenv (newdir);            /* fetch newdir from environment*/
  55.         if (tmp == NULL) {
  56.             strcpy (errtext,"$");
  57.             strcat (errtext, newdir);
  58.             strcat (errtext," undefined.");
  59.             puts (errtext);
  60.             _exit();
  61.         }
  62.         strcpy(newdir,tmp);
  63.     }
  64.  
  65.     newchdir (newdir);                    /* now do the extended chdir    */
  66. }
  67.  
  68. /*
  69. **    newchdir extends chdir. the default drive is changed
  70. **    when given in dirname
  71. */        
  72.  
  73. newchdir(dirname)
  74. char dirname[];
  75.  
  76. {
  77.  
  78.     int drive, newdrive;
  79.     char testchar;
  80.     char errtext[64];
  81.  
  82.     testchar = dirname[1];
  83.     if (testchar == ':') {                /* is a drive included ?             */
  84.         testchar = toupper(dirname[0]); /* yes !                             */
  85.         newdrive = (int)(testchar-'A');    /* here we want to go ...             */
  86.         drive = getdisk();                /* and here we are !                */
  87.         if (newdrive != drive) {         /* we have to change the drive ...    */
  88.             setdisk (newdrive);            /* done !                            */
  89.         }
  90.     }
  91.  
  92.     if (chdir(dirname)) {
  93.         if (wildchdir (dirname)) {
  94.             strcpy(errtext, "\"");
  95.             strcat(errtext,dirname);
  96.             strcat(errtext, "\": no such directory");
  97.             puts (errtext);             /* WHOOPS!                    */
  98.                                         /* to save space, was : perror ("cdd")     */
  99.  
  100.             if (newdrive != drive) {     /* we changed the drive ...            */
  101.                 setdisk (drive);        /* now come back !                    */
  102.             }
  103.  
  104.             _exit();
  105.         }
  106.     }
  107.  
  108.  
  109. }    
  110.  
  111. /*
  112. **    now the incarnation :
  113. **    try to find a directory with wildcards (tough!)
  114. */
  115.  
  116. int wildchdir (dirname)
  117. char dirname[];
  118.  
  119. {
  120.     struct ffblk ff;
  121.  
  122.     int done;
  123.     int bad = -1;
  124.     char drive[MAXDRIVE];
  125.     char dir[MAXDIR];
  126.     char file[MAXFILE];
  127.     char ext[MAXEXT];
  128.     char newpath[64];
  129.  
  130.     fnsplit(dirname, drive, dir, file, ext);         /* watch this!             */
  131.                                                     /* we need it later     */
  132.     done = findfirst(dirname, &ff, 0x10);
  133.     if (done)                                        /* that's easy !        */
  134.         return (bad);
  135.     if (ff.ff_attrib & 0x10) {                /*   vv-- we need one more arg!    */
  136.         fnmerge(newpath, drive, dir, ff.ff_name, ""); /* here it is !        */
  137.         if (ff.ff_name[0] != '.')                    /* that would be stupid */
  138.             bad = chdir (newpath);
  139.     }
  140.  
  141.     if (bad) {
  142.         while ((!done) && (bad)) {
  143.             done = findnext (&ff);
  144.             if (ff.ff_attrib & 0x10) {                /* never cd to a file!    */
  145.                 fnmerge(newpath, drive, dir, ff.ff_name, "");
  146.                 if (ff.ff_name[0] != '.')
  147.                     bad = chdir (newpath);
  148.             }
  149.         }
  150.     }
  151.     return (bad);
  152.  
  153. }
  154.  
  155. /*
  156. **    some kind of MID$ in BASIC
  157. **    moveleft ("moveleft",3,1) gives "meleft"
  158. **    moveleft ("$TMPDIR",1,0) gives "TMPDIR"
  159. */
  160.  
  161. moveleft (st,from,to)
  162. char *st;
  163. int from;
  164. int to;
  165.  
  166. {
  167.      for (;(st[to] = st[from]) != '\0'; from++,to++);
  168. }
  169.  
  170. /*
  171. **    a workaround-the-bug
  172. */
  173.  
  174. char *getenv(s)
  175. char *s;
  176.  
  177. {
  178.     char *p, *e;
  179.     char **env;
  180.  
  181.     env = environ;
  182.     while(1) {
  183.         e = *env++;
  184.         if (e == (char *) 0)
  185.             break;
  186.         for (p = s; *p == *e; p++, e++) ;
  187.         if  (*p == 0 && *e == '=')
  188.             return (char *)++e;
  189.         while (*e)
  190.             e++;
  191.         e++;
  192.     }
  193.     return ((char *) e);
  194.  
  195. }
  196.